package com.shop.user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.shop.user.po.OrderDetail;
import com.shop.user.po.Orders;
import com.shop.user.po.dto.OrderInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author banxia
 * @since 2023-02-25
 */
public interface OrderMapper extends BaseMapper<Orders> {

    @Update("update orders set discount_id=#{discountId},discount_price=#{discountPrice}," +
                            "need_amount=total_amount-#{discountPrice} where id=#{orderId} " +
                            "and total_amount>=#{minPrice} ")
    int changeDiscount(@Param("orderId") Long orderId,
                       @Param("discountId") Long discountId,
                       @Param("minPrice") Long minPrice,
                       @Param("discountPrice") Long discountPrice);

    @Select("select * from orders where id=#{orderId}")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "orderId",column = "order_id"),
            @Result(property = "userId",column = "user_id"),
            @Result(property = "addressId",column = "address_id"),
            @Result(property = "state",column = "state"),
            @Result(property = "totalAmount",column = "total_amount"),
            @Result(property = "discountId",column = "discount_id"),
            @Result(property = "discountPrice",column = "discount_price"),
            @Result(property = "discountAmount",column = "discount_amount"),
            @Result(property = "remarks",column = "remarks"),
            @Result(property = "orderDetailList",column = "order_id",
                    javaType = List.class,
                    many = @Many(select = "com.shop.user.mapper.OrderMapper.getOrderDetailByOrderId"))
    })
    OrderInfo getOrderInfo(@Param("orderId") Long orderId);

    @Select("select * from order_detail where order_id=#{orderId}")
    public List<OrderDetail> getOrderDetailByOrderId(@Param("orderId") Long orderId);
}
